package org.je.image;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 * @author fanyifeng 2012-11-5
 */
public abstract class ImageCanvas extends Canvas
{
	private BufferedImage	tempCanvas;

	private int				_w;
	private int				_h;

	/**
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param watchSpeed
	 */
	public ImageCanvas(int x, int y, int w, int h) {
		setBounds(x ,y ,w ,h);
		this._w = w;
		this._h = h;
		this.tempCanvas = ImageUtil.createCompatibleImage(w ,h ,1);
	}

	public int w() {
		return _w;
	}

	public int h() {
		return _h;
	}

	@Override
	public void update(Graphics g) {
		paint(g);
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D tempG = tempCanvas.createGraphics();
		draw2D(tempG);
		tempG.dispose();
		g.drawImage(tempCanvas ,0 ,0 ,null);
	}

	/**
	 * @param g
	 */
	public abstract void draw2D(Graphics2D g);

	private static final long	serialVersionUID	= 1L;
}
